//
//  AppDelegate.m
//  微博
//
//  Created by MS on 15-12-3.
//  Copyright (c) 2015年 MS. All rights reserved.
//

#import "AppDelegate.h"
#import "UIImageView+WebCache.h"

#import "LGOAuthViewController.h"
#import "LGAccountTool.h"
#import "LGRootTool.h"

#import <AVFoundation/AVFoundation.h>
#import "LGTabbarController.h"
#import "LGNavigationController.h"
#import "LGProfileController.h"
#import "LGTabBar.h"
//偏好设置的好处
/*
 1.不需要关注文件名
 2.快速进行键值对得存储
 
 
 */

@interface AppDelegate ()
@property (nonatomic,strong) AVAudioPlayer * player;
@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after applicationlaunch.
    
    
    UIUserNotificationSettings * setting = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge categories:nil];
    [application registerUserNotificationSettings:setting];
    
    
    //真机上后台播放要设置音频会话
    AVAudioSession * session = [AVAudioSession sharedInstance];
    
    //设置会话类型（后台播放）
    [session setCategory:AVAudioSessionCategoryPlayback error:nil];
    [session setCategory:AVAudioSessionCategorySoloAmbient error:nil];
    
    //激活
    [session setActive:YES error:nil];
    
    //创建窗口
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    
    //选择跟控制器
    //是否授权-->进行授权
    if ([LGAccountTool account]) {//已授权
        
        [LGRootTool chooseRootViewController:self.window];
    }
    
    else{//授权
        
        LGOAuthViewController * oauthVC = [[LGOAuthViewController alloc] init];
        self.window.rootViewController = oauthVC;
    }
    
    //设置窗口的跟控制器
    
    [self.window makeKeyAndVisible];
    
    return YES;
}

//使用SDWebImage要注意清内存
//接受到内存警告时调用
-(void) applicationDidReceiveMemoryWarning:(UIApplication *)application
{
    //停止所有下载
    [[SDWebImageManager sharedManager] cancelAll];
    
    //删除缓存
    [[SDWebImageManager sharedManager].imageCache clearMemory];
    
    
}

- (void)applicationWillResignActive:(UIApplication *)application {
   
   //失去焦点时播放
    NSURL * url = [[NSBundle mainBundle] URLForResource:@"silence.mp3" withExtension:nil];
    AVAudioPlayer * palayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url  error:nil];
    
    [palayer prepareToPlay];
    //无限播放
    palayer.numberOfLoops = -1;
    [palayer play];
    _player = palayer;

}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    
    //开启一个后台程序,优先级低
  UIBackgroundTaskIdentifier ID = [application beginBackgroundTaskWithExpirationHandler:^{
      [application endBackgroundTask:ID];
      
  }];
    
    
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

//应用间跳转
-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    
    LGTabbarController * tabVc = (LGTabbarController * ) self.window.rootViewController;
    NSInteger  count =tabVc.childViewControllers.count;
    NSString * str = [url absoluteString];
    
    LGTabBar * tab = nil;
    for (UIView  * view in tabVc.tabBar.subviews) {
        if ([view isKindOfClass:[LGTabBar class]]) {
             tab =( LGTabBar *)view;
        }
    }

    if ([str rangeOfString:@"profile"].length) {
        
        tabVc.selectedIndex =count-1 ;
        [tab selectBtn:count-1];
        
    }
    else if([str rangeOfString:@"discover"].length ){
        
        tabVc.selectedIndex =count-2;
        [tab selectBtn:count-2];

    }
    else{
        tabVc.selectedIndex =0;
        [tab selectBtn:0];
    }
    return YES;
}

@end
